/**
 * 
 */
package com.uit.project.bustrip.bo;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.uit.project.bustrip.appconfig.AppsConfig;
import com.uit.project.bustrip.bointerface.PointBO;
import com.uit.project.bustrip.dao.PointDaoImpl;
import com.uit.project.bustrip.daointerface.PointDao;
import com.uit.project.bustrip.entity.Point;
import com.uit.project.bustrip.ultil.JSONUltil;
/**
 * @author 
 *
 */
public class PointBOImpl extends BaseBOImpl<Point, Integer> implements PointBO{
	public PointBOImpl(){
		super();
	}
	public PointBOImpl(Context context){
		super(context);
	}
	@Override
	public Point getInstance() {
		List<Point> point = ((PointDao)this.getDao()).findAll();
		if(point.size() > 0)
			return point.get(0);
		return null;
	}
	@Override
	public List<Point> getAllPointByTripAsync(Integer tripId) {
		String url = getByTripURL(tripId);
		JSONObject arrays = JSONUltil.getJSONObjectFromURL(url);
		if (arrays != null) {
			try {
				JSONArray jsonArray = arrays
						.getJSONArray(AppsConfig.JSON_OBJECT);
				PointDao pointDao = new PointDaoImpl();
				List<Point> seats = new ArrayList<Point>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = (JSONObject) jsonArray.get(i);
					Point seat = pointDao.getFromJSON(jsonObject);
					seats.add(seat);
				}
				return seats;
			} catch (Exception e) {
				return null;
			}
		}
		return null;
	}
	private String getByTripURL(Integer trip){
		return AppsConfig.getServiceURL()
				+ "?_module=Point&_view=json&_action=list&_Tripid=" + trip;
	}
}
